//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace OpenLS.Drawing.UI.Internals
{
    public class ColorBaseToBrushConverter: IMultiValueConverter
    {
        
        static ColorScheme _oldScheme;
        static Dictionary<ColorBase, Brush> _cache = new Dictionary<ColorBase, Brush>();
        public static Brush ColorBaseToBrush(ColorBase color, ColorScheme colorScheme) 
        {
            if (_oldScheme != colorScheme)
            {
                _cache = new Dictionary<ColorBase, Brush>();
            }
            _oldScheme = colorScheme;
            if (color == null)
                return Brushes.Pink;
            ColorBase key = color;
            Brush result;
            if (_cache.TryGetValue(key, out result))
                return result;
            result = new SolidColorBrush(color.ResolveColor(colorScheme));
            result.Freeze();
            _cache.Add(key, result);
            return result;
        }

        #region IMultiValueConverter Members

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            object result =  ColorBaseToBrush(StrictConverter.Convert<ColorBase>(values[0]), StrictConverter.Convert<ColorScheme>(values[1]));
            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}